<template>
  <div class="setting">
    <Card :dis-hover="true">
      <Form ref="form_data" :model="form_data" :rules="rule_form_data">
        <FormItem label="用户素材库默认内存(单位M)" prop="default_size">
          <Input v-model.number="form_data.default_size" placeholder="用户素材库默认内存(单位M)"></Input>
        </FormItem>
        <FormItem label="视频时长限制(单位:秒)" prop="duration">
          <Input v-model.number="form_data.duration" placeholder="视频时长限制(单位:秒)"></Input>
        </FormItem>
        <FormItem label="是否审核素材" prop="is_check">
          <i-switch v-model="form_data.is_check" />
        </FormItem>
        <FormItem label="视频是否转码" prop="transcoding">
          <i-switch v-model="form_data.transcoding" />
        </FormItem>
        <FormItem>
          <Button type="primary" @click="submit('form_data')">确定</Button>
        </FormItem>
      </Form>
    </Card>
  </div>
</template>

<script>
export default {
  name: 'base-setting',
  data () {
    return {
      form_data: {
        default_size: 1024,
        duration: 30,
        is_check: true,
        transcoding: false
      },
      rule_form_data: {
        default_size: [
          { type: 'number', max: 9999, message: '不能大于9999M', trigger: 'blur' }
        ],
        duration: [
          { type: 'number', max: 3600, message: '不能大于1小时', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    data () {
      let { default_size, duration, is_check, transcoding } = this.form_data
      return {
        default_size,
        duration,
        is_check: is_check ? 1 : 0,
        transcoding: transcoding ? 1 : 0
      }
    }
  },
  mounted () {
    this.find()
  },
  methods: {
    // Run
    /**
     * 获取参数
     */
    find () {
      this.$get('settingMedia/find')
        .then((res) => {
          console.log(res)
          let { code, data } = res.data
          if (code === 0) {
            let { default_size, duration, is_check, transcoding } = data
            this.form_data = {
              default_size: parseInt(default_size),
              duration: parseInt(duration),
              is_check: is_check === '1',
              transcoding: transcoding === '1'
            }
          }
        })
    },

    // Handle
    /**
     * 提交设置参数
     */
    submit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.$post('settingMedia/set', {
            data: this.data
          })
            .then((res) => {
              if (res.data.code === 0) {
                this.$Message.success('设置成功')
              } else {
                this.$Message.error('设置失败')
              }
            })
        }
      })
    }
  }
}
</script>
